// pages/myAttention/myAttention.js
var app=getApp();
var fileData = require('../../utils/data.js');
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    imageUrl: app.globalData.imageUrl,
    userInfo: '',
    tabData: [
      { txt: '个人', current: true },
      { txt: '活动', current: false },
      { txt: '顺风车', current: false },
      // { txt: '相亲', current: false }
    ],
    showSelect:false,
    actiItem0: [],
    actiItem1: [],
    actiItem2: [],
    actiItem3: [],
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData1: true,
    hasData2: true,
    hasData0: true,
    hasData3: true,
    page1: 1,
    page2: 1,
    page0: 1,
    page3: 1,
    current: 0,
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    this.getAttentionGr(true);
    this.getAttentionSfc(true);
    this.getAttentionHd(true);
    this.getAttentionXq(true);
  },
  enterPerson: function (e) {
    wx.navigateTo({
      url: '/pages/personalProfile/personalProfile?id=' + e.target.id
    })
  },
  showDetail: function (e) {
    wx.navigateTo({
      url: '/pages/carDetail/carDetail?isMine=true&id=' + e.currentTarget.dataset.item.id
    })
  },
  showDetailXq: function (e) {
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/blindDetail/blindDetail?id=' + item.id + '&userId=' + item.userId
    })
  },
  setLove: function (e) {//设置取消心动
    var item = e.currentTarget.dataset.item;
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'xqUserBasicInfo/updateXqUserCharmValue', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        userId: this.data.userInfo.id,
        loveFlag: item.loveFlag == 'Y' ? 'N' : 'Y',
        otherId: item.userId
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            ['actiItem[' + e.currentTarget.dataset.index + '].loveFlag']: item.loveFlag == 'Y' ? 'N' : 'Y'
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  leaveWord: function (e) {//
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/blindDetail/blindDetail?isLeave=true&id=' + item.id + '&userId=' + item.userId
    })
  },
  getAttentionGr:function(flag){
    var _this=this;
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getMyAttention', //仅为示例，并非真实的接口地址
      data: {
        userId: this.data.userInfo.id,
        currentPage: this.data.page0,
        pageNumber: '10',
        block:1
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.data.userInfos){
          if (res.data.data.userInfos.length < 10) {
            _this.setData({
              hasData0: false
            })
          } else {
            _this.setData({
              hasData0: true
            })
          }
        }
        
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem0: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        
        if (res.statusCode==200){
          if (res.data.data.userInfos) {
            _this.data.actiItem0 = _this.data.actiItem0.concat(res.data.data.userInfos);
          }
        }
        _this.setData({
          actiItem0: _this.data.actiItem0
        })
      }
    })
  },
  getAttentionHd: function (flag) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getMyAttention',
      data: {
        currentPage: this.data.page2,
        pageNumber: '10',
        userId: _this.data.userInfo.id,
        block: 2
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem1: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        if (res.data.data.activities) {
          if (res.data.data.activities.length < 10) {
            _this.setData({
              hasData1: false
            })
          } else {
            _this.setData({
              hasData1: true
            })
          }
          res.data.data.activities.forEach(function (ele) {
            if (ele.releaseTime) {
              ele.time = util.getDays(ele.releaseTime);
            }
          })
          _this.data.actiItem1 = _this.data.actiItem1.concat(res.data.data.activities);
        }
        
        _this.setData({
          actiItem1: _this.data.actiItem1
        })
      }
    })
  },
  // 顺风车
  getAttentionSfc:function(flag){
    var _this = this;
    var obj = { '车找人': 1, '人找车': 2, '货找车': 3, '车找货': 4};
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getMyAttention', //仅为示例，并非真实的接口地址
      data: {
        userId: this.data.userInfo.id,
        currentPage: this.data.page2,
        pageNumber: '10',
        block: 3
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem2: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        if (res.data.data.cars) {
          if (res.data.data.cars.length < 10) {
            _this.setData({
              hasData2: false
            })
          } else {
            _this.setData({
              hasData2: true
            })
          }
          res.data.data.cars.forEach(function (ele) {
            ele.type = obj[ele.typeName];
            if (ele.createTime) {
              ele.time = util.getDays(ele.createTime)
            }
            if (ele.labelArray != undefined && ele.labelArray != '') {
              ele.mark = ele.labelArray.split('/');
            } else {
              ele.mark = [];
            }
          })
          _this.data.actiItem2 = _this.data.actiItem2.concat(res.data.data.cars);
        }
        _this.setData({
          actiItem2: _this.data.actiItem2
        })
      }
    })
  },
  // 相亲
  getAttentionXq: function (flag) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getMyAttention',
      data: {
        currentPage: this.data.page2,
        pageNumber: '10',
        userId: _this.data.userInfo.id,
        block: 4
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem3: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        if (res.data.data.datas) {
          if (res.data.data.datas.length < 10) {
            _this.setData({
              hasData3: false
            })
          } else {
            _this.setData({
              hasData3: true
            })
          }
          _this.data.actiItem3 = _this.data.actiItem3.concat(res.data.data.datas);
        }
        _this.setData({
          actiItem3: _this.data.actiItem3
        })
      }
    })
  },
  swichTab:function (e) {
    this.data.tabData.forEach(function (ele) {
      ele.current = false;
    })
    this.data.tabData[e.currentTarget.dataset.value].current = true;
    this.setData({
      tabData: this.data.tabData,
      current: parseInt(e.currentTarget.dataset.value)
    })
  },
  showShopIndex:function(e){
    wx.navigateTo({
      url: '/pages/shopIndex/shopIndex',
    })
  },
  callShop: function (e) {
    var show = e.currentTarget.dataset.value == "true" ? true : false;
    this.setData({
      showSelect: show
    })
  },
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.value //仅为示例，并非真实的电话号码
    })
    this.setData({
      showSelect: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      loadSuccess: false
    })
    wx.showLoading()
    this.setData({
      ['page' + this.data.current]: 1
    })
    if (this.data.current == 0) {
      this.getAttentionGr(true);
    } else if (this.data.current == 1) {
      this.getAttentionHd(true);
    } else if (this.data.current == 2) {
      this.getAttentionSfc(true);
    } else {
      this.getAttentionXq(true);
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      ['page' + this.data.current]: 1
    })
    if (this.data.current == 0) {
      this.getAttentionGr(true);
    } else if (this.data.current == 1) {
      this.getAttentionHd(true);
    } else if (this.data.current == 2) {
      this.getAttentionSfc(true);
    } else {
      this.getAttentionXq(true);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data['hasData' + this.data.current]) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        ['page' + this.data.current]: this.data['page' + this.data.current] + 1
      });
      if (this.data.current == 0) {
        this.getAttentionGr();
      } else if (this.data.current == 1) {
        this.getAttentionHd();
      } else if (this.data.current == 2) {
        this.getAttentionSfc();
      } else {
        this.getAttentionXq();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})